package com.sgb.kjwl.viewmodel.machine;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LeaseQuedPriceEvent;
import com.sgb.kjwl.view.ui.activity.leaseAsk.UpdateLeaseQuedPriceActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UpdateLeaseQuePriceModel extends BaseViewModel {

    private UpdateLeaseQuedPriceActivity askLeaseActivity;

    public UpdateLeaseQuePriceModel(UpdateLeaseQuedPriceActivity askLeaseActivity) {
        this.askLeaseActivity = askLeaseActivity;
    }

    //更改报价
    public void changePrice(String rentalSeekId, String newPrice, String remark, String quoteUtil, String quoteKey,String categoryType) {
        DialogHelper.showProgressDialog(askLeaseActivity, null, "更改报价中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("rentalSeekId", rentalSeekId);
        map.put("quoteUtil", quoteUtil);
        map.put("quoteKey", quoteKey);
        map.put("quotePrice", newPrice);
        map.put("remark", remark);
        map.put("categoryType", categoryType);//机械，1设备 2车辆，3房产，4土地 5工厂 6农机
        APIManager.getInstance().getMachineAPI().changMyQuote(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    //更改报价成功
                    MToast.showToast(askLeaseActivity, "更改报价成功");
                    EventBus.getDefault().postSticky(LeaseQuedPriceEvent.make().setMessageTag(EventConstant.EVENT_MY_PRICE_LIST));
                    askLeaseActivity.finish();

                } else {
                    MToast.showToast(askLeaseActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(askLeaseActivity, "更改报价失败");
            }
        });
    }

    /**
     * 机械求租--价格单位
     */
    public void getUnit() {
        DialogHelper.showProgressDialog(askLeaseActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getMachineAPI().getUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseActivity.doPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 设备求租--价格单位
     */
    public void getRentpriceUnit() {
        DialogHelper.showProgressDialog(askLeaseActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getMachineAPI().getRentpriceUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseActivity.doPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 车辆求租--价格单位
     */
    public void getNeedLeaseCarPrice() {
        DialogHelper.showProgressDialog(askLeaseActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getMachineAPI().getNeedLeaseCarPrice().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseActivity.doPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 房产求租--价格单位
     */
    public void getEstatePriceUnit() {
        DialogHelper.showProgressDialog(askLeaseActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getMachineAPI().getEstatePriceUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseActivity.doPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取土地价格单位
     */
    public void getPriceUnitModel() {
        DialogHelper.showProgressDialog(askLeaseActivity,  "数据加载中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getPriceUnitLand().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseActivity.doPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

//    /**
//     * 判断求租信息能否被报价（机械求组，设备求租，房产求租，车辆求租）
//     *
//     * @param rentSeekId
//     * @param updateTime
//     */
//    public void judgeImValid(String rentSeekId, String updateTime) {
//        DialogHelper.showProgressDialog(askLeaseActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
//        APIManager.getInstance().getMachineAPI().judgeImValid(rentSeekId, updateTime).enqueue(new Callback<BaseEntity>() {
//            @Override
//            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
//                DialogHelper.dismissProgressDialog();
//                try {//接口返回数据不标准,只能自行转换
//                    JSONObject jsonObject = new JSONObject(response.body().getData().toString());
//                    String str = jsonObject.getString("data");
//                    //0可报价 1 不可报价
//                    if ("1".equals(str)) {
//                        MToast.showToast(askLeaseActivity, "当前信息已变更,请返回刷新重新报价");
//                    } else {
//                        askLeaseActivity.toJudgeImValid();
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                    MToast.showToast(askLeaseActivity, "当前信息已变更,请返回刷新重新报价");
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//            }
//        });
//    }

}
